package com.jss.lanouhn.fristapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.Interface.OnItemClickLister;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.CategoryStrategyNormalAdapter;
import com.jss.lanouhn.fristapp.modles.CategoryStrategyNormal;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CategoryLastActivity extends AppCompatActivity {
    private String url1 = "http://api.liwushuo.com/v2/channel_groups/all";
    private CategoryStrategyNormal categoryStrategtNormal;
    private RecyclerView recyclerView;
    private CategoryStrategyNormalAdapter adapter;
    private TextView textView;
    private ImageView iv_back,iv_select;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_last);
        textView= (TextView) findViewById(R.id.tv_category_last);
        iv_back= (ImageView) findViewById(R.id.btn_back_credit3);
        iv_select= (ImageView) findViewById(R.id.iv_category_last);
        recyclerView = (RecyclerView) findViewById(R.id.rv_category_last);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url1)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(CategoryLastActivity.this, "没有请求到数据", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        categoryStrategtNormal = gson.fromJson(result, CategoryStrategyNormal.class);
                        if (categoryStrategtNormal.getData().getChannel_groups().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            Toast.makeText(CategoryLastActivity.this, "网络解析数据错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle=getIntent().getExtras();
            final int fly=bundle.getInt("fly");
            if(msg.what==1){
                //去找第一个页面的适配器，和搜索页面的适配器，哪个能用用哪个，创建新的activity，多次加载使用
                textView.setText(categoryStrategtNormal.getData().getChannel_groups().get(fly).getName());
                adapter=new CategoryStrategyNormalAdapter(2,fly,categoryStrategtNormal,CategoryLastActivity.this);
                adapter.setOnItemClickLister(new OnItemClickLister() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent=new Intent(CategoryLastActivity.this,CategoryRealLastActivity.class);
                        Bundle bundle1=new Bundle();
                        bundle1.putInt("lastId",categoryStrategtNormal.getData().getChannel_groups().get(fly).getChannels().get(position).getId());
                        bundle1.putString("lastName",categoryStrategtNormal.getData().getChannel_groups().get(fly).getChannels().get(position).getName());
                        intent.putExtras(bundle1);
                        startActivity(intent);
                    }
                });
                recyclerView.setAdapter(adapter);
            }
        }
    };
}
